package com.groot.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.groot.common.util.MedicalDBUtil;
import com.groot.medical.entity.MedicalInstitution;


public class MedicalDaoImpl implements MedicalDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	@Override
	public int getAllMenuCount() {
		int i =0;
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
		conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
		String sql="SELECT count(1) AS id FROM t_medicalinstitution";
		ps=conn.prepareStatement(sql);
		rs=ps.executeQuery();
		if(rs.next()){
			i=rs.getInt("id");
		}
		} catch (SQLException e) {

		e.printStackTrace();
		}
		finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return i;
	}

	@Override
	public List<MedicalInstitution> findMenuByPage(int startIndex, int pageSize) {
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql = "select * from t_medicalinstitution limit ?,?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, startIndex);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			List<MedicalInstitution> medicalList = new ArrayList<>();
			
			while(rs.next()){
				/*sysUser = new SysUser(rs.getString(2), rs.getString(3), rs.getString(4));*/
				MedicalInstitution medical = new MedicalInstitution();
				
				medical.setId(rs.getInt(1));
				medical.setMedicalId(rs.getString(2));
				medical.setMedicalName(rs.getString(3));
				medical.setMedicalAddress(rs.getString(4));
				
				medicalList.add(medical);
				
			}
			
			return medicalList;
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
	}

	@Override
	public int addMedical(MedicalInstitution medical) {
		int i =0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="insert into t_medicalinstitution(medical_id,medical_name,medical_address)values(?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1, medical.getMedicalId());
			ps.setString(2,medical.getMedicalName());
			ps.setString(3,medical.getMedicalAddress());
			
			i= ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return i;
	}

	@Override
	public MedicalInstitution findMedicalInfoById(String id) {
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*PreparedStatement ps = null;
		ResultSet rs = null;*/
		
		String sql = "select * from t_medicalinstitution where id=?";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			/*SysUser sysUser = null;*/
			if(rs.next()){
				/*sysUser = new SysUser(rs.getString(2), rs.getString(3), rs.getString(4));*/
				MedicalInstitution medical = new MedicalInstitution();
				medical.setId(rs.getInt(1));
				medical.setMedicalId(rs.getString(2));
				medical.setMedicalName(rs.getString(3));
				medical.setMedicalAddress(rs.getString(4));
				
				return medical;
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new  RuntimeException("出现错误！");
		}finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return null;
	}

	@Override
	public int editMedical(MedicalInstitution medical) {
		int i =0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			//UPDATE student SET name='"+name+"', age=" + age+" where name='大红'";
			String sql="update t_medicalinstitution set medical_id=?,medical_name=?,medical_address=? where id=?";
			ps=conn.prepareStatement(sql);
			
			ps.setString(1, medical.getMedicalId());
			ps.setString(2,medical.getMedicalName());
			ps.setString(3,medical.getMedicalAddress());
			ps.setInt(4, medical.getId());
			i= ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return i;
	}

	@Override
	public int CheckNameExist(String medicalname) {
		int i =0;
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
		conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
		String sql="SELECT count(1) AS id FROM t_medicalinstitution WHERE medical_name=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, medicalname);
		rs=ps.executeQuery();
		if(rs.next()){
			i=rs.getInt("id");
		}
		} catch (SQLException e) {

		e.printStackTrace();
		}
		finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return i;
	}

}
